﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Input;
using Bixas.Tangibilis.Input;
using System.ComponentModel;
using System.Windows.Threading;
using System.Security;
using Bixas.Tangibilis.Utility;
using System.Diagnostics;


namespace Bixas.Tangibilis
{
    public class TouchCursor : DispatcherObject
    {
        private int _cursorID;
        private double _Left;
        private double _Top;
        private double _OldLeft;
        private double _OldTop;
        private IInputElement _Captured;
        private CaptureMode _captureMode;
        private IInputElement _cursorOver;
        private WeakReference _rawCursorOver;
        private bool _isUpdateFirst;
        private TouchCursorWindow _window;

        public TouchCursor()
        { //this._window = new TouchCursorWindow(); }
        }
        ~TouchCursor()
        {
            this.Dispose();
            GC.SuppressFinalize(this);
        }

        public int cursorID
        {
            get { return _cursorID; }
            set { _cursorID = value; }
        }

        internal TouchCursorWindow Window
        {
            get { return _window; }
            set { _window = value; }
        }

        public double Left
        {
            get { return _Left; }
            set { _Left = value; }
        }
        public double Top
        {
            get { return _Top; }
            set { _Top = value; }
        }

        public double OldLeft
        {
            get { return _OldLeft; }
            set { _OldLeft = value; }
        }

        public double OldTop
        {
            get { return _OldTop; }
            set { _OldTop = value; }
        }

        public IInputElement Captured
        {
            get { return _Captured; }
            set { _Captured = value; }
        }

        public IInputElement CursorOver
        {
            get { return _cursorOver; }
            set { _cursorOver = value; }
        }

        public CaptureMode CaptureMode
        {
            get { return _captureMode; }
            set { _captureMode = value; }
        }

        public WeakReference RawCursorOver
        {
            get { return _rawCursorOver; }
            set { _rawCursorOver = value; }
        }
        internal bool IsUpdateFirst
        {
            get { return _isUpdateFirst; }
            set { this._isUpdateFirst = value; }
        }

        public void CaptureElement(IInputElement element)
        {
            this.CaptureElement(element, CaptureMode.Element);
        }

        public void CaptureElement(IInputElement element, CaptureMode captureMode)
        {
            this.Captured = element;
            this.CaptureMode = captureMode;
        }

        public void ReleaseCapture()
        {
            this.Captured = null;
            this.CaptureMode = CaptureMode.None;
        }

        public void SetPosition(int x, int y)
        {
            // Point topPoint = TouchSource.getInstance().CurrentWindow.PointToScreen(new Point(0, 0));
            // double newX = topPoint.X + x - (this.Window.ActualWidth / 2);
            // double newY = topPoint.Y + y - (this.Window.ActualHeight / 2);

            // WindowInteropHelper helper = new WindowInteropHelper(this._window);
            //    NativeMethods.SetWindowPos(helper.Handle, IntPtr.Zero, (int)newX, (int)newY, 0, 0, 0x1);
        }

        public void Synchronize()
        {
            RawTouchCursorInputEventArgs args = new RawTouchCursorInputEventArgs(this, Environment.TickCount, null, RawTouchCursorOperation.Update);
            args.RoutedEvent = TouchDevice.PreviewInputReportEvent;
            args.IsPreviewEvent = true;
            Debug.Write("toto");
            Debug.WriteLine(args.Timestamp.ToString());
            InputManager.Current.ProcessInput(args);
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.Dispose(true);
        }

        private delegate void CloseWindowDelegate();

        public virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.Window != null)
                {
                    this.Window.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new CloseWindowDelegate(this.CloseWindow));
                    this.Window = null;
                }
            }
        }

        public Point GetPosition(IInputElement relativeTo)
        {

            return TouchDevice.GetPosition(new Point(_Left, _Top), relativeTo);

        }

        private void CloseWindow()
        {
            this.Window.Close();
        }
        #endregion
    }
}



